package main

import (
	"fmt"
	"os"
	"time"

	"gitee.com/go-mod/framework/config"
)

// cd cmd/server go run .
func main() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err)
		}
	}()
	config.Init()

	if len(os.Args) > 1 {
		runCli()
	} else {
		runApp()
	}
}

func runApp() {
	app, cleanup, err := InitApp()
	if err != nil {
		panic(err)
	}
	defer cleanup()

	// Run HTTP gRPC
	app.Run(config.GetString("http.addr"), config.GetString("grpc.addr"))

	// 优雅关闭
	app.Shutdown(5*time.Second, 5*time.Second)
}

func runCli() {
	app, cleanup, err := InitCli()
	if err != nil {
		panic(err)
	}
	defer cleanup()

	app.Cli.Run()
}
